package com.gopay.checkorder.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.checkorder.service.BankOrderService;
import com.gopay.checkorder.service.task.BankCountDownLatch;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.BisBankInterfaceChannel;
import com.gopay.common.constants.bank.CmbConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BankOrder;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cmb.ebank.query.CmbB2bQueryReq;
import com.gopay.common.domain.bank.cmb.ebank.query.CmbB2bQueryRes;
import com.gopay.common.domain.bank.dayend.BaseDayendEntry;
import com.gopay.common.domain.cashier.BankCardType;
import com.gopay.common.domain.cps.vo.CheckOrderResult;
import com.gopay.common.domain.cps.vo.InAccInfo;
import com.gopay.common.util.DateUtils;
import com.gopay.remote.bank.BankInteractRemoteService;

/** 
 * @ClassName: CheckOrderCmbB2bImpl 
 * @Description: 招商银行b2b 对单 
 * @author w_jiqing 
 * @date 2016年9月2日 上午9:45:13  
 */ 
public class CheckOrderCmbB2bImpl implements BankOrderService{

    private static Logger logger = LoggerFactory.getLogger(CheckOrderCmbB2bImpl.class);
    @Resource(name = "bankClient")
    private BankInteractRemoteService bankClient;
    @Override
    public CheckOrderResult checkOrder(BankOrder bankOrder, String target) {
        CheckOrderResult checkOrderResult = new CheckOrderResult();
        checkOrderResult.setOrderId(bankOrder.getOrderId());
        
        try{
            CmbB2bQueryReq message = new CmbB2bQueryReq();
            message.setOrderId(bankOrder.getOrderId());
            message.setBankOrderId(bankOrder.getOutOrderId());
            message.setBank(BankConstants.BANK_LOGO_CMB);
            message.setBankIntrChann(BisBankInterfaceChannel.get(bankOrder.getInterfaceChennelCode()));
            message.setBankBusProd(BisBankBusinessProd.get(bankOrder.getBankBizProdCode()));
            message.setOrderDate(DateUtils.format(bankOrder.getSendBankTm(),"yyyyMMdd"));
            
            Result result =new Result();        
            result =bankClient.query(message);
            if(result == null || !result.isSuccess() || result.getDefaultModel() == null){
                logger.error("bankClient fail.");   
                if(result ==null || ProcCodeConstants.PROC_CODE_100E6036.equals(result.getResultCode())){
                    BankCountDownLatch.failIncrease(bankOrder.getBankCode());
                    checkOrderResult.setCommunicateError(true);
                }
                checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E6022);
                checkOrderResult.setErrMsg("系统返回对账结果为空");
                return checkOrderResult;
            }
            
            CmbB2bQueryRes res = (CmbB2bQueryRes) result.getDefaultModel();
            if( res == null){
                // 查询订单为空
                logger.error("fail:orderId:" + bankOrder.getOrderId()+ " errcode:" + ProcCodeConstants.PROC_CODE_100E6022+ " err:bis query is null.");
                checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E6022);
                checkOrderResult.setErrMsg("银行返回对账结果为空");
                return checkOrderResult;
            }
            if(res.getNtqtrdqyz() != null){
                if(CmbConstants.CMBB2B_QUERY_STATUS_FIN.equals(res.getNtqtrdqyz().getOrdordsta())){
                    String ordstscod = res.getNtqtrdqyz().getOrdstscod();  
                    //成功处理
                    if(CmbConstants.CMBB2B_QUERY_STATUS_SUCCESS.equals(ordstscod)){
                        checkOrderResult.setSuccess(true);
                        InAccInfo accInfo = new InAccInfo();
                        accInfo.setOrderId(res.getNtqtrdqyz().getOrdreford());
                        accInfo.setAmount(new BigDecimal(res.getNtqtrdqyz().getTrsamt()));
                        accInfo.setRetCode("00");
                        accInfo.setCardType(BankCardType.Unknown.toString());
                        checkOrderResult.setAccInfo(accInfo);
                        logger.info("招商银行B2B订单:"+res.getNtqtrdqyz().getOrdreford()+",交易返回信息:订单支付成功, 查询到的订单结果 :"+ordstscod);
                    }else if(CmbConstants.CMBB2B_QUERY_STATUS_FAIL.equals(ordstscod)){
                        //失败处理
                        checkOrderResult.setBankFailure(true);
                        checkOrderResult.setErrMsg("支付失败");
                        logger.info("招行银行B2B订单:"+res.getNtqtrdqyz().getOrdreford()+",交易返回信息:订单支付失败, 查询到的订单结果 :"+ordstscod);
                    }else if(CmbConstants.CMBB2B_QUERY_NO_PAY.equals(ordstscod)){
                        //待缴费
                        checkOrderResult.setErrMsg("处理中");
                        logger.info("招行银行B2B订单:"+res.getNtqtrdqyz().getOrdreford()+",交易返回信息:代缴费, 查询到的订单结果 :"+ordstscod);
                    }else if(CmbConstants.CMBB2B_QUERY_PAY_NO_SUCCESS.equals(ordstscod)){
                        //多收方支付不成功
                        checkOrderResult.setErrMsg("处理中");
                        logger.info("招行银行B2B订单:"+res.getNtqtrdqyz().getOrdreford()+",交易返回信息:多收方支付不成功, 查询到的订单结果 :"+ordstscod);
                    }else{
                        //订单失效
                        checkOrderResult.setBankFailure(true);
                        checkOrderResult.setErrMsg("订单失效");
                        logger.info("招行银行B2B订单:"+res.getNtqtrdqyz().getOrdreford()+",交易返回信息:订单失效, 查询到的订单结果 :"+ordstscod);
                    }
                }else {
                    checkOrderResult.setErrMsg("处理中");
                    logger.info("招行银行B2B订单:"+res.getNtqtrdqyz().getOrdreford()+",交易返回信息:订单请求状态非 FIN状态, 查询到的订单请求状态 :"+res.getNtqtrdqyz().getOrdordsta());
                } 
            }else{
                checkOrderResult.setErrMsg("处理中");
            }
            
            
        } catch(Exception e){
            logger.error("cmb b2b bankorder fail.",e);
            checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E5062);
        }
        return checkOrderResult;
    }

    @Override
    public BankOrder getDbOrder() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public BaseMessage dayend(String bank_bus) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result getDayendMsg(String string) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<CheckOrderResult> checkDayend(String bank_bus) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public CheckOrderResult checkDayendOrder(BaseDayendEntry bdm, BankOrder bo) {
        // TODO Auto-generated method stub
        return null;
    }

}
